package com.example.lambda;

import com.google.common.base.Strings;
import com.google.common.collect.Lists;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Supplier;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;

/**
 * @author fxb
 * @date 2018/3/20 20:27
 * @Description: TODO
 */
public class TentativeTest {

    public static void main(String[] args) {
//        listFilter();
//        OfMethodDemo();
//        generateMethodDemo();
        iterateMethodDemo();
//        arrayMap();



    }


    // 创建Stream --->>> 转换Stream --->>> 聚合
    public static void listFilter(){
        List<Integer> nums = Lists.newArrayList(1, null, 3, 4, null, 6);
        long count = nums.stream().filter(num -> num != null).count();
        System.out.println(count);
    }



    /**
     *  创建Stream
     *  1. 通过Stream接口的静态工厂方法(Java8 接口里可以带静态方法)
     *  2. 通过Collection 接口的默认方法, (默认方法: Default method, 也是Java8 中的一个新特性, 就是接口中的一个带有实现的方法)
     *      -stream(), 把一个Collection对象转换成Stream
     */
    public static void OfMethodDemo(){
        Stream<Integer> integerStream = Stream.of(1,2,3,4,5);
        Stream<String> stringStream = Stream.of("tao", "bao");

        System.out.println(integerStream+"^");
        System.out.println(stringStream+"*");
    }

    public static void generateMethodDemo(){

        Stream.generate(new Supplier<Double>() {
            @Override
            public Double get() {
                return Math.random();
            }
        });
        Stream.generate(()->Math.random());
        Stream.generate(Math::random);
    }

    public static void iterateMethodDemo(){
        Stream.iterate(1, item -> item + 1).limit(10).forEach(System.out::println);
    }


    public static void arrayMap(){
        String [] array = {"a", "b", "c"};
        for (Integer i : Lists.newArrayList(1, 2, 3)) {
            Stream.of(array).map(item -> Strings.padEnd(item, i, '@')).forEach(System.out::println);
        }
    }







}
